package com.lgkj.ui;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.view.CountDownTimerButton;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/27 0027.
 */

public class ForgetPwdActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_ver)
    EditText etVer;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.cb_pwd)
    CheckBox cbPwd;
    @BindView(R.id.btn_ver)
    CountDownTimerButton btnVer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.iv_back, R.id.btn_ver, R.id.cb_pwd, R.id.ibtn_save, R.id.tv_xieyi})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_ver:
                getVer();
                break;
            case R.id.cb_pwd:
                if (cbPwd.isChecked()) {
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                etPassword.setSelection(etPassword.getText().toString().length());
                break;
            case R.id.ibtn_save:
                forgetPwd();
                break;
            case R.id.tv_register:
                break;
        }
    }

    private void forgetPwd() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ForgetPwdActivity.this));
        map.put("phone", etPhone.getText().toString());
        map.put("password", etPassword.getText().toString());
        map.put("verify", etVer.getText().toString());
        HttpUtils.postHttp(map, CommonConstant.changePassword, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    toastLong("密码修改成功", 0);
                    finish();
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    private void getVer() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ForgetPwdActivity.this));
        map.put("mobile", etPhone.getText().toString());
        HttpUtils.postHttp(map, CommonConstant.getver, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    toastLong("短信验证码发送成功", 0);
                    btnVer.start();
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

}
